#include "Greedy.h"

int Greedy::nodeToExplore() {
  int nid;
  if (!q.empty()) {
    nid = q.top().Val2;
    q.pop();
  } else {
    nid = network->getTeleportNode();
    updateDiscoveredNode(nid);
  }
  return nid;
}

void Greedy::updateDiscoveredNode(int discoveredNode) {
  if (visit.find(discoveredNode) == visit.end()) {
    TIntPr deg = network->getDegree(discoveredNode);
    q.push(TIntPr(deg.Val1, discoveredNode));
    visit[discoveredNode] = 1;
  }
}

#ifdef _GREEDY_TEST_

int main(int argc, char* argv[]) {
  const int MAX_COST = 10000;
  API *api = new API(1000);

  Greedy greed(api);
  greed.setCostLimit(MAX_COST);
  while (greed.getTotalCost() < MAX_COST) {
    greed.nextStep();
    TIntPr p = greed.evaluate();
    cout << "Greedy cost used: " 
	 << greed.getTotalCost() 
	 << "  node recall: " 
	 << p.Val1  << endl;

    if (p.Val1 == 1000)
      break;
  }

  return 0;
}

#endif
